﻿#region BarChartCylindars UI Control
//
// All rights are reserved. Reproduction or transmission in whole or in part, in 
// any form or by any means, electronic, mechanical or otherwise, is prohibited 
// without the prior written consent of the copyright owner.
// 
// Filename: BarChartCylindars.xaml.cs
//
#endregion using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscoveryLogic.UI.Layouts.Controls;

namespace DiscoveryLogic.UI.Layouts.Controls
{
    /// <summary>
    /// User Control to display stacked cylindars
    /// </summary>
    public partial class BarChartCylindars : UserControl
    {
        /// <summary>
        /// holder for position of minimum payment line
        /// </summary>
        private double minimumPaymentLevel;

        /// <summary>
        /// Constructor
        /// </summary>
        public BarChartCylindars()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Change the size of the principal cylindar based on change in cylindar top position
        /// </summary>
        /// <param name="deltaHeight">Change in hieght in 1/100"</param>
        /// <returns>true if successful</returns>
        public bool AlterCylindar(double deltaHeight)
        {
            bool isValid = false;
            double newCylTop = 0;
            double newCylGlassTop = 0;
            double newCylBodyHeight = 0;
            double newCylBodyTop = 0;
            //minimumPaymentLevel = 0;
            Ellipse cylindarTop = PrincipalTop;
            Ellipse cylindarGlassTop = PrincipalGlassTop;
            Rectangle cylindarBody = PrincipalBarrel;
            Ellipse minPaymentLevelDisk = InterestTop;
            
            newCylTop = (double)cylindarTop.GetValue(Canvas.TopProperty);
            newCylGlassTop = (double)cylindarGlassTop.GetValue(Canvas.TopProperty);
            newCylBodyHeight = (double)cylindarBody.Height;
            newCylBodyTop = (double)cylindarBody.GetValue(Canvas.TopProperty);
            
            newCylTop += deltaHeight;
            newCylGlassTop += deltaHeight;
            newCylBodyTop += deltaHeight;
            newCylBodyHeight -= deltaHeight;

            //if (newCylTop <= minimumPaymentLevel)
            //{
                cylindarGlassTop.SetValue(Canvas.TopProperty, newCylGlassTop);
                cylindarTop.SetValue(Canvas.TopProperty, newCylTop);
                cylindarBody.Height = newCylBodyHeight;
                cylindarBody.SetValue(Canvas.TopProperty, newCylBodyTop);

                ////Set text labels                
                PrincipalText.SetValue(Canvas.TopProperty, newCylBodyTop - PrincipalText.Height/2);

                isValid = true;

            //}
            return isValid;
        }

        /// <summary>
        /// Sets the height of the cylindars based on an arbitrary set of heights
        /// </summary>
        /// <param name="principalPos">position of the top of the principal cylindar</param>
        /// <param name="interestPos">position of the top of the interest cylindar</param>
        /// <param name="minPaymentLevel">position of the minimum payment line.</param>
        /// <returns></returns>
        public bool SetCylindar(double principalPos, double interestPos, double minPaymentLevel)
        {
            //When looking at calculations, remember top left corner is {0,0} and down and right are positive changes.
            //Set up the locals
            bool isValid = false;
            double newCylTop = 0;
            double newCylGlassTop = 0;
            double newCylBodyHeight = 0;
            double newCylBodyTop = 0;
            double newCylBottom = 0;
            double newCylInterestTop = 0;
            double newCylInterestBodyTop=0;
            double newCylInterestBodyHeight=0;
            double newCylInterestBottom=0;
            
            //Get the objects associated to the shapes
            Ellipse cylindarTop = PrincipalTop;
            Ellipse cylindarGlassTop = PrincipalGlassTop;
            Rectangle cylindarBody = PrincipalBarrel;
            Ellipse cylindarBottom = PrincipalBase;
            Ellipse cylindarInterestTop = InterestTop;
            Rectangle cylindarInterestBody = InterestBarrel;
            Ellipse cylindarInterestBottom = InterestBase;
            
            //Get the bottom disk position from the xaml as that won't change
            newCylInterestBottom = (double)cylindarInterestBottom.GetValue(Canvas.TopProperty) + (cylindarInterestBottom.Height/2);

            double graphMinPosition = newCylInterestBottom;

            //set an offset to overlap the ellipses
            double offset = 0.5 * cylindarInterestTop.Height - 5;

            //set the minimum payment level value
            minimumPaymentLevel = newCylInterestBottom - minPaymentLevel + offset;
            
            
            //// calculate interest bar positions
            newCylInterestTop = newCylInterestBottom - interestPos - (cylindarInterestTop.Height/2);
            newCylInterestBodyTop = newCylInterestBottom - interestPos;
            newCylInterestBodyHeight = interestPos;


            // calculate principal bar positions
            newCylBottom = newCylInterestTop - 1;

            newCylBodyHeight = principalPos - interestPos;
            newCylTop = newCylBottom - newCylBodyHeight;
            newCylGlassTop = newCylTop;
            
            newCylBodyTop = newCylBottom + (cylindarBottom.Height/2) - newCylBodyHeight;
            
                       
            ////Set the shape position values
            cylindarGlassTop.SetValue(Canvas.TopProperty, newCylGlassTop);
            cylindarTop.SetValue(Canvas.TopProperty, newCylTop);
            cylindarBottom.SetValue(Canvas.TopProperty, newCylBottom);
            cylindarBody.Height = newCylBodyHeight;
            cylindarBody.SetValue(Canvas.TopProperty, newCylBodyTop);
            cylindarInterestTop.SetValue(Canvas.TopProperty, newCylInterestTop);
            cylindarInterestBody.SetValue(Canvas.TopProperty, newCylInterestBodyTop);
            cylindarInterestBody.SetValue(Canvas.HeightProperty, newCylInterestBodyHeight);

            ////Set text labels            
            PrincipalText.SetValue(Canvas.TopProperty, newCylBodyTop - PrincipalText.Height/2);
            InterestText.SetValue(Canvas.TopProperty, newCylInterestBodyTop - InterestText.Height/2);

            isValid = true;
            return isValid;
        }

       
    }
}
